package service.impl;

import bean.*;
import mapper.IStudentMapper;
import mapper.IUserMapper;
import org.apache.ibatis.session.SqlSession;
import service.IUserService;

import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {


    @Override
    public UserBean login(String userName, String password) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> userList = mapper.findAll();
        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getUsername().equals(userName) &&
                    userList.get(i).getPassword().equals(password)) {
                UserBean user = mapper.login(userName, password);
                session.close();
                return user;
            }
        }
        return null;
    }

    @Override
    public CutPageBean<UserBean> cutAll(int pageNo) {
        CutPageBean<UserBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        //封装当前页的数据
        cutBean.setList(mapper.cutByList((pageNo - 1) * CutPageBean.PAGESIZE,
                CutPageBean.PAGESIZE));
        //封装总记录数
        cutBean.setCount(mapper.getTotal());
        session.close();
        return cutBean;
    }

    @Override
    public boolean add(UserBean user) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> userList = mapper.findAll();
        for (int i = 0; i < userList.size(); i++) {
            if (user.getUsername().equals(userList.get(i).getUsername())) {
                return false;
            }
        }
        mapper.add(user);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public void delete(int userId) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delete(userId);
        session.commit();
        session.close();
    }

    @Override
    public boolean update(Integer userId, String newGrade) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(userId);
        if (newGrade.equals(user.getLevel())) {
            return false;
        } else {
            mapper.update(userId, newGrade);
        }
        session.commit();
        session.close();
        return true;
    }

    @Override
    public UserBean findById(int userId) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(userId);
        session.close();
        return user;
    }

    @Override
    public boolean changePwd(Integer userId, String newPwd, String surePwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(userId);
        if (newPwd.equals(user.getPassword())) {
            return false;
        }
        if (!newPwd.equals(surePwd)) {
            return false;
        }else {
            mapper.changePwd(userId, newPwd);
        }
        session.commit();
        session.close();
        return true;
    }


}
